import Vue from 'vue';
import App from './App.vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import "babel-polyfill";
import "es6-promise-polyfill";


//路由信息
import VueRouter from 'vue-router';
import routes from './router/routes.js';

//请求配置信息
import axios from 'axios';
import qs from 'qs';
import { post, fetch, patch, put } from './common/js/http.js';
import { getCookie, setCookie, delCookie } from './common/js/cookie.js';

// import { setLocalStorage, isQuotaExceeded, getLocalStorage } from './common/js/localstorage.js';

// import { showChat } from './api/api';

// 指令
import '@/directives/enter'

// 引入配置
import './plugin/index';
import './filters/index';

//mint-ui 框架引入
import Mint from 'mint-ui';
import 'mint-ui/lib/style.css';

//引入项目中公共的样式
import './common/css/reset.css';
import './common/css/global.css';
import './common/icon/font/iconfont.css';

//引入icon库
import './common/icon/font/iconfont.js';

//引入jq
import $ from 'jquery'

//引入自适应屏幕的 rem
import './common/js/public.js';

//导入手机控制台
// import VConsole from 'vconsole/dist/vconsole.min.js' //import vconsole
// var vConsole = new VConsole() // 初始化

// 时间格式化过滤器
// import '@/filters/index';

//引入vuex
import store from './store/index'

// 拖拽组件
import VueDND from 'awe-dnd'
Vue.use(VueDND)

// 仿原生搜索页面
// import 'vue-search-bar/dist/vue-search-bar.min.css';
// import { SearchBar } from 'vue-search-bar';
// Vue.use(SearchBar);


//引入Mint
Vue.use(Mint);

Vue.use(ElementUI)



//定义全局变量
Vue.prototype.$post = post;
Vue.prototype.$fetch = fetch;
Vue.prototype.$patch = patch;
Vue.prototype.$put = put;
Vue.prototype.$store = store;
// Vue.prototype.$getCookie = getCookie;

// Vue.prototype.$setLocalStorage = setLocalStorage;
// Vue.prototype.$isQuotaExceeded = isQuotaExceeded;
// Vue.prototype.$getLocalStorage = getLocalStorage;
Vue.prototype.$axios = axios;
// Vue.prototype.token    =  1111;
const access_token = window.localStorage.getItem('access_token');

//定义设置后台请求地址
Vue.prototype.$path = 'http://fc.x.e000.com';
// Vue.prototype.$path = 'http://www.ooox.cn';

//定义消息列表
Vue.prototype.$systemlist;

// 定义全局百度地图ak
Vue.prototype.$baidu_map_ak = 'n5xrUsfxlpvH3cQrKCEGlWZcGF9uehkT';

//当前用户操作系统
Vue.prototype.$system;

Vue.use(VueRouter)


const router = new VueRouter({
        mode: 'history', //去掉url中的#
        routes
        // scrollBehavior(to, from, savedPosition) {
        //     if (savedPosition) {
        //         return savedPosition
        //     } else {
        //         return {
        //             x: 0,
        //             y: 0
        //         }
        //     }
        // }
    })
    // 全局路由守卫
router.beforeEach((to, from, next) => {
    store.commit('setPage', 1)

    const nextRoute = ['CreateCircle', 'company', 'userinfo', 'cart', 'profile', 'information', 'UserCard', 'crcircle', 'companyCreate', 'companyData', 'JoinCircle', 'userCenter', 'editCard', 'createCard'];

    let access_token = window.localStorage.getItem('access_token'); //是否有token

    let access_token_status = false;

    // if (!getCookie('appid')) {
    //     axios.get('http://fc.x.e000.com/index.php/api/yield').then((response) => {
    //         setCookie('appid', response.data, 1);
    //     })
    // }

    // let appid = getCookie('appid');


    // if (!window.localStorage.getItem('token')) {

    //     fetch('http://fc.x.e000.com/index.php/api/yieldtoken', { appid: appid }).then((response) => {
    //         window.localStorage.setItem('token', response);
    //     })

    // }






    // 未登录状态；当路由到nextRoute指定页时，跳转至login
    if (nextRoute.indexOf(to.name) >= 0) {
        // console.log(access_token);
        // console.log(access_token);

        if (!access_token) {
            //不存在 就直接调登陆 生成
            // console.log('what fuck');
            next({ path: '/login' });

        } else {
            //存在的情况 分两种 一种过期  一种没过期

            //这里是登录令牌Token
            const access = window.localStorage.getItem('access_ token') || '';
            //当前时间
            const nowTime = Date.now();
            //这里是过期时间。
            // 如果没有 登录时间的话，设置当前为登录时间
            let accessExpTimer = Number(window.localStorage.getItem('timer')); //过期的阈值(判断时间是否过期时间一定要number类型不然无法相加)
            let exp = 1000 * 60 * 60 * 2;

            let Expired = accessExpTimer + exp;


            // let appid = 


            // console.log(typeof nowTime);
            // console.log(typeof accessExpTimer);

            //当时间大于登录时间2小时时候，登录过期
            if (nowTime > (exp + accessExpTimer) || accessExpTimer == null) {
                //处理过期的事情
                localStorage.removeItem('access_token');
                next({ path: '/login' });
            } else {

                //处理未过期的事情
                //没有过期正常走流程
                axios.get('http://fc.x.e000.com/index.php/api/verification/' + access_token).then((response) => {
                    // console.log(response);
                    let code = response.data.code;
                    if (code == 200) {
                        //如果已经登录则每次跳转都需要更新当前时间
                        window.localStorage.setItem('timer', Date.now());

                        access_token_status = true;
                        // store.commit('setPage', 2);
                        // if (!window.localStorage.getItem('chat_id')) {
                        // showChat().then(res => {
                        //     window.localStorage.setItem('chat_id', res.chat.chat_id);
                        //     console.log('查看聊天对象:', res);
                        // })
                        // }
                        // console.log(response.data.data);
                        // if(response.data.data == 1){ 
                        //      next();
                        // }else{ 
                        //    next({path:'/companyBind'});
                        // }
                        next();



                    } else {
                        access_token_status = false;

                        next({ path: '/login' });

                        // console.log('进来了验证失败');

                    }

                })
            }

        }


    } else {

        if (to.name === 'login' && access_token) {
            // console.log('到这里来了');
            next({ path: '/' });
        } else {
            //如果已经登录则每次跳转都需要更新当前时间
            window.localStorage.setItem('timer', Date.now());
            // console.log('走这里来了');
            next();
        }
        // console.log('这里来了3333');
        //  next();

    }

    // console.log('token不为空');
    // 已登录状态；当路由到login时，跳转至home 
    // if (to.name === 'login' && isLogin) {
    //   console.log('跳进来了');
    //   if (access_token != '' ) {
    //     next({ path: '/' });
    //   }
    // }

});




/***定义全局函数*****/
Vue.prototype.getUserInfo = function() {
    let access_token = window.localStorage.getItem('access_token');
    if (access_token == null) {

    } else {
        let url = 'http://fc.x.e000.com/index.php/api/userinfo/' + access_token;
        let url1 = 'http://fc.x.e000.com/index.php/api/Get_user_info';
        return axios.get(
                url)
            .then((res) => {
                console.log(res);
                return (res.data.data);
            })
    }

}

Vue.prototype.GetSystem = function() {
    var u = navigator.userAgent.toLowerCase();
    // console.log(u);

    if (/android/i.test(u)) {
        this.$system = 'android';

    } else if (/iphone/i.test(u)) {

        this.$system = 'ios';

    } else {
        this.$system = 'linux';
    }

}

// plugin/route.js
// router.afterEach((to, from) => {

//     var u = navigator.userAgent.toLowerCase();
//     console.log(u);

//     if (/android/i.test(u)) {
//         this.$system = 'android';

//     } else if (/iphone/i.test(u)) {

//         this.$system = 'ios';

//     } else {
//         this.$system = 'linux';
//     }

//     console.log('操作系统', this.$system);
// })








new Vue({
    router,
    el: '#app',
    render: h => h(App)
})